package com.wtc.service;

import com.wtc.dao.EmployeeDao;
import com.wtc.entity.Employee;
import com.wtc.utils.MybatisUtility;
import com.wtc.utils.Utility;
import org.apache.ibatis.session.SqlSession;


import java.io.IOException;
import java.util.List;

/**
 * 编写用于操作数据表的功能，包括新增、修改、删除、查询所有员工功能。
 */

public class EmployeeService {
    /**
     * 查询方法
     * @throws IOException
     */
    public void select() throws IOException {
        //使用动态代理并调用接口中的方法
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        EmployeeDao dao = sqlSession.getMapper(EmployeeDao.class);
        //将查询结果保存在集合中
        List<Employee> employeeList = dao.select();
        //输出查询结果
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
        //关闭sqlSession连接
        sqlSession.close();
    }

    /**
     * 新增方法
     * @throws IOException
     */
    public void insert() throws IOException {
        //使用动态代理并调用接口中的方法
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        EmployeeDao dao = sqlSession.getMapper(EmployeeDao.class);
        //创捷变量接收各个添加信息
        System.out.print("请输入员工ID：");
        int employee_id = Utility.readInt();
        System.out.print("请输入员工姓名：");
        String employee_name = Utility.readString(10);
        System.out.print("请输入员工类型：");
        String employee_type = Utility.readString(10);
        System.out.print("请输入员工性别：");
        String employee_sex = Utility.readString(2);
        System.out.print("请输入员工部门：");
        String employee_depart = Utility.readString(10);
        //创建存储对象
        Employee employee = new Employee(employee_id, employee_name, employee_type, employee_sex, employee_depart);
        //执行插入add
        dao.add(employee);
        //提交事务
        sqlSession.commit();
        System.out.println("添加成功！");
        //关闭 SqlSession对象连接
        sqlSession.close();
    }

    /**
     * 修改方法
     * @throws IOException
     */
    public void update() throws IOException {
        //使用动态代理并调用接口中的方法
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        EmployeeDao dao = sqlSession.getMapper(EmployeeDao.class);
        System.out.println("以下是可修改的员工信息：");
        //调用查询方法
        select();
        //创建变量接收各个修改信息
        System.out.print("请输入要修改信息的员工ID：");
        int employee_id = Utility.readInt();
        System.out.print("请输入修改后的员工姓名：");
        String employee_name = Utility.readString(10);
        System.out.print("请输入修改后的员工类型：");
        String employee_type = Utility.readString(10);
        System.out.print("请输入修改后的员工性别：");
        String employee_sex = Utility.readString(2);
        System.out.print("请输入修改后的员工部门：");
        String employee_depart = Utility.readString(10);
        //创建保存数据的对象
        Employee employee = new Employee(employee_id, employee_name, employee_type, employee_sex, employee_depart);
        //执行更新 update
        dao.update(employee);
        //提交事务
        sqlSession.commit();
        System.out.println("修改成功！");
        //关闭 SqlSession
        sqlSession.close();
    }

    /**
     * 删除方法
     * @throws IOException
     */
    public void delete() throws IOException {
        //使用动态代理使用接口中的方法
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        EmployeeDao dao = sqlSession.getMapper(EmployeeDao.class);
        System.out.println("以下是可删除的员工信息：");
        //调用查询方法
        select();
        System.out.print("请输入你想要删除员工的ID：");
        //保存要删除的指定ID
        int employee_id = Utility.readInt();
        //执行删除 delete
        dao.delete(employee_id);
        //提交事务
        sqlSession.commit();
        System.out.println("删除成功！");
        //关闭 SqlSession
        sqlSession.close();
    }
}
